home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / trapz < prev    next >
Text File  |  1995-03-20  |  739b  |  31 lines

  1. //
  2. //  a = trapz(y,x)
  3. //
  4. //  find the area under the curve y = f(x) using the trapezoidal rule
  5. //
  6. //   x = vector containing the independent variable data points
  7. //       the elements of x need not be equally spaced
  8. //   y = f(x) = vector of the function values corresponding to the
  9. //       elements in x
  10. //
  11. //  a = the area under f(x) in the interval defined by x
  12. //
  13.  
  14. // Written by: Duane Hanselman, University of Maine, (207)-581-2246
  15.  
  16. trapz = function (y, x)
  17. {
  18.   local (x, y)
  19.  
  20.   x = x[:]';  // make sure x and y are row vectors 
  21.   y = y[:]';
  22.   ly = length(y);
  23.   lx = length(x);
  24.   if (lx != ly) {
  25.     error(" x and y must be the same length");
  26.   }
  27.   a = 0.5*([0, y] + [y, 0]) .* ([x, 0] - [0, x]);
  28.   a = sum (a[1;2:ly]);
  29.   return a;
  30. };
  31.